package org.tloss.web.editor.sql.editors;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.layout.TreeColumnLayout;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.CheckboxTreeViewer;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.DialogCellEditor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.TreeViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;
import org.tloss.web.editor.Utils;
import org.tloss.web.editor.enity.editors.JavaClassDialogEditor;
import org.tloss.web.editor.model.Param;
import org.tloss.web.editor.model.Sql;
import org.tloss.web.editor.utils.Constants;

public class ParamEditor extends EditorPart {

	IResourceChangeListener dirtyListener;

	public void setDirtyListener(IResourceChangeListener dirtyListener) {
		this.dirtyListener = dirtyListener;
	}

	Sql sql = null;

	public void setSql(Sql sql) {
		this.sql = sql;
		treeViewer.setInput(sql);
		treeViewer.refresh();
	}

	CheckboxTreeViewer treeViewer;

	public void doSave(IProgressMonitor monitor) {

	}

	public void doSaveAs() {

	}

	public void init(IEditorSite site, IEditorInput input)
			throws PartInitException {
		if (!(input instanceof IFileEditorInput)) {
			PartInitException exception =  new PartInitException("Input must be a file");
			Utils.logError("Input must be a file", exception);
			throw  exception;
		}
		setSite(site);
		setInput(input);
	}

	public boolean isDirty() {
		return false;
	}

	public boolean isSaveAsAllowed() {

		return false;
	}

	protected void updateTree() {
		treeViewer.refresh();
		if (dirtyListener != null) {
			dirtyListener.resourceChanged(null);
		}
	}

	public void createPartControl(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		composite.setLayout(layout);
		layout.numColumns = 2;

		Composite treeContainer = new Composite(composite, SWT.BORDER);
		GridData gridData = new GridData();
		treeContainer.setLayoutData(gridData);
		gridData.horizontalAlignment = SWT.FILL;
		gridData.grabExcessHorizontalSpace = true;
		gridData.verticalAlignment = SWT.FILL;
		gridData.grabExcessVerticalSpace = true;
		TreeColumnLayout treeLayout = new TreeColumnLayout();
		treeContainer.setLayout(treeLayout);
		treeViewer = new CheckboxTreeViewer(treeContainer, SWT.MULTI
				| SWT.FULL_SELECTION);
		Tree tree = treeViewer.getTree();
		tree.setHeaderVisible(true);
		treeViewer.setContentProvider(new ParamListContentProvider());

		TreeColumn valueColumn;
		valueColumn = new TreeColumn(tree, SWT.NONE);
		valueColumn.setText("Param");
		treeLayout.setColumnData(valueColumn, new ColumnWeightData(2));
		TreeViewerColumn column2 = new TreeViewerColumn(treeViewer, valueColumn);
		column2.setEditingSupport(new EditingSupport(treeViewer) {
			TextCellEditor editor = new TextCellEditor((Composite) treeViewer
					.getControl());

			@Override
			protected boolean canEdit(Object arg0) {

				return true;
			}

			@Override
			protected CellEditor getCellEditor(Object arg0) {
				return editor;
			}

			@Override
			protected Object getValue(Object arg0) {
				if (arg0 != null && arg0 instanceof Param) {
					return ((Param) arg0).getName();
				}
				return "";
			}

			@Override
			protected void setValue(Object arg0, Object arg1) {
				if (arg0 != null && arg0 instanceof Param) {
					((Param) arg0).setName(arg1.toString());
					updateTree();
				}

			}
		});

		column2.setLabelProvider(new ColumnLabelProvider() {
			public String getText(Object element) {
				if (element instanceof Param) {
					return ((Param) element).getName();
				}
				return "";
			}

			public Image getImage(Object element) {

				return Constants.PARAM_IMAGE;
			}
		});

		TreeColumn valueColumn2;
		valueColumn2 = new TreeColumn(tree, SWT.NONE);
		valueColumn2.setText("Value");
		treeLayout.setColumnData(valueColumn2, new ColumnWeightData(2));
		TreeViewerColumn column = new TreeViewerColumn(treeViewer, valueColumn2);
		column.setEditingSupport(new EditingSupport(treeViewer) {
			TextCellEditor editor = new TextCellEditor((Composite) treeViewer
					.getControl());

			@Override
			protected boolean canEdit(Object arg0) {

				return true;
			}

			@Override
			protected CellEditor getCellEditor(Object arg0) {
				return editor;
			}

			@Override
			protected Object getValue(Object arg0) {
				if (arg0 != null && arg0 instanceof Param) {
					return ((Param) arg0).getValue() != null ? ((Param) arg0)
							.getValue() : "";
				}
				return "";
			}

			@Override
			protected void setValue(Object arg0, Object arg1) {
				if (arg0 != null && arg0 instanceof Param) {
					((Param) arg0).setValue(arg1.toString().equals("") ? null
							: arg1.toString());
					updateTree();
				}

			}
		});

		column.setLabelProvider(new ColumnLabelProvider() {
			public String getText(Object element) {
				if (element instanceof Param) {
					return ((Param) element).getValue() != null ? ((Param) element)
							.getValue() : "";
				}
				return "";
			}

			public Image getImage(Object element) {

				return null;
			}
		});

		TreeColumn valueColumn3;
		valueColumn3 = new TreeColumn(tree, SWT.NONE);
		valueColumn3.setText("Type");
		treeLayout.setColumnData(valueColumn3, new ColumnWeightData(2));
		TreeViewerColumn column3 = new TreeViewerColumn(treeViewer,
				valueColumn3);
		column3.setEditingSupport(new EditingSupport(treeViewer) {

			DialogCellEditor cellEditor = new JavaClassDialogEditor(
					(Composite) treeViewer.getControl()) ;
			@Override
			protected boolean canEdit(Object arg0) {

				return true;
			}

			@Override
			protected CellEditor getCellEditor(Object arg0) {
				return cellEditor;
			}

			@Override
			protected Object getValue(Object arg0) {
				if (arg0 != null && arg0 instanceof Param) {
					return ((Param) arg0).getType() != null ? ((Param) arg0)
							.getType() : "";
				}
				return "";
			}

			@Override
			protected void setValue(Object arg0, Object arg1) {
				if (arg0 != null && arg0 instanceof Param) {
					((Param) arg0).setType(arg1.toString().equals("") ? null
							: arg1.toString());
					updateTree();
				}

			}
		});

		column3.setLabelProvider(new ColumnLabelProvider() {
			public String getText(Object element) {
				if (element instanceof Param) {
					return ((Param) element).getType() != null ? ((Param) element)
							.getType() : "";
				}
				return "";
			}

			public Image getImage(Object element) {

				return null;
			}
		});

		getSite().setSelectionProvider(treeViewer);

		treeViewer.setInput(sql);

		treeViewer.setAutoExpandLevel(TreeViewer.ALL_LEVELS);

		Composite wrapper = new Composite(composite, SWT.NONE);
		RowLayout rowLayout = new RowLayout();
		wrapper.setLayout(rowLayout);
		rowLayout.type = SWT.VERTICAL;

		final Button addButton = new Button(wrapper, SWT.NONE);
		addButton.setText("Add");

		addButton.addMouseListener(new MouseListener() {

			public void mouseUp(MouseEvent e) {

			}

			public void mouseDown(MouseEvent e) {
				List<Param> params = sql.getParams();
				if (params == null) {
					params = new ArrayList<Param>();
					sql.setParams(params);
				}
				Param param = new Param("Type name here", null);
				params.add(param);
				treeViewer.refresh();
			}

			public void mouseDoubleClick(MouseEvent e) {

			}
		});
		final Button deleteButton = new Button(wrapper, SWT.NONE);
		deleteButton.setText("Delete");

		deleteButton.addMouseListener(new MouseListener() {

			public void mouseUp(MouseEvent e) {

			}

			public void mouseDown(MouseEvent e) {
				Object[] objects = treeViewer.getCheckedElements();
				if (objects != null) {
					List<Param> params = sql.getParams();
					if (params != null) {
						for (int i = 0; i < objects.length; i++) {
							params.remove(objects[i]);
						}
					}
					updateTree();
				}

			}

			public void mouseDoubleClick(MouseEvent e) {

			}
		});

		final Button button = new Button(wrapper, SWT.NONE);
		button.setText("Select All");

		button.addMouseListener(new MouseListener() {

			public void mouseUp(MouseEvent e) {

			}

			@SuppressWarnings("deprecation")
			public void mouseDown(MouseEvent e) {
				treeViewer.setAllChecked(true);
			}

			public void mouseDoubleClick(MouseEvent e) {

			}
		});
		final Button button2 = new Button(wrapper, SWT.NONE);
		button2.setText("Deselect All");

		button2.addMouseListener(new MouseListener() {

			public void mouseUp(MouseEvent e) {

			}

			@SuppressWarnings("deprecation")
			public void mouseDown(MouseEvent e) {
				treeViewer.setAllChecked(false);
			}

			public void mouseDoubleClick(MouseEvent e) {

			}
		});
	}

	public void setFocus() {

	}

}
